package com.wan.util;

import java.util.*;
import java.util.concurrent.*;

/**
 * @author wwh
 * @date 2022/7/7 10:57
 */
public class SafeUtil {

    private static final HashSet<Class<?>> safeMapTypes = new HashSet<>();
    private static final HashSet<Class<?>> safeCollectionTypes = new HashSet<>();
    static {
        //map
        safeMapTypes.add(ConcurrentMap.class);
        safeMapTypes.add(ConcurrentHashMap.class);
        safeMapTypes.add(ConcurrentSkipListMap.class);
        safeMapTypes.add(Hashtable.class);
        //集合
        safeCollectionTypes.add(CopyOnWriteArrayList.class);
        safeCollectionTypes.add(CopyOnWriteArraySet.class);
        safeCollectionTypes.add(ConcurrentSkipListSet.class);
        safeCollectionTypes.add(Vector.class);
        //队列
        safeCollectionTypes.add(ConcurrentLinkedQueue.class);
        safeCollectionTypes.add(ConcurrentLinkedDeque.class);
        safeCollectionTypes.add(ArrayBlockingQueue.class);
        safeCollectionTypes.add(LinkedBlockingDeque.class);
        safeCollectionTypes.add(LinkedBlockingQueue.class);
        safeCollectionTypes.add(LinkedTransferQueue.class);
        safeCollectionTypes.add(SynchronousQueue.class);
        safeCollectionTypes.add(PriorityBlockingQueue.class);
        safeCollectionTypes.add(DelayQueue.class);
    }

    /**
     * 是否是线程安全的Map
     */
    public static boolean isSafeMap(Class<? extends Map<?,?>> mapType){
        return safeMapTypes.contains(mapType);
    }

    /**
     * 是否是线程安全的集合
     */
    public static boolean isSafeCollection(Class<? extends Collection<?>> collType){
        return safeCollectionTypes.contains(collType);
    }

}
